package com.flute.haflute.jobbox.master;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

import com.flute.haflute.client.TransferredFiles;
import com.flute.haflute.jobbox.base.ClientJobListener;
import com.flute.haflute.jobbox.base.JobConf;
import com.flute.haflute.jobbox.base.JobSubmitter;
import com.flute.haflute.jobbox.common.TransferredFileWriter;
import com.flute.haflute.jobbox.communication.NotifiyDaemon;
import com.flute.haflute.jobbox.communication.messages.FileTransferMessage;

/**
 * 平台内部提交任务的代理类，隔离外界与JobTracker的直接通信，同时
 * 
 */
public class JobClient extends UnicastRemoteObject implements JobSubmitter {
	private static final long serialVersionUID = 0L;

	public JobClient() throws RemoteException {

	}

	@Override
	public void submit(JobConf jobConf, ClientJobListener listener) {
		JobTracker.getInstance().executeNewJob(jobConf, listener);
	}

	@Override
	public void transferFiles(TransferredFiles transferredFiles)
			throws RemoteException {
		FileTransferMessage message = new FileTransferMessage();
		message.setContent(transferredFiles);
		// 在MASTER上保存文件
		new TransferredFileWriter().writeContentFromMessage(message);
		// 通知各个SLAVE节点保存文件
		NotifiyDaemon.getInstance().broadcast(message);
	}

}
